/**
 * Project: topdt-jbpm
 * Copyright 2013  Top Digital Technology  Co. Ltd.
 * All right reserved.
 */
package com.topdt.jbpm.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.thx.common.orm.HibernateBaseDao;
import com.thx.common.util.AppContext;
import com.topdt.jbpm.model.WorkAttach;

/**
 * 建立日期 : 2013-4-25 下午12:03:16<br>
 * 作者 : balance<br>
 * 模块 : <br>
 * 描述 : <br>
 * 修改历史: 序号 日期 修改人 修改原因 <br>
 * 1 2016-11-16 Xuejw 代码重构<br>
 * 2 <br>
 */
@Repository
public class AttachmentDao extends HibernateBaseDao<WorkAttach> {
	/**
	 * 根据工作id取得附件列表 
	 * 建立日期 : June 25, 2013 5:41:50 PM<br>
	 * 作者 : wangamin<br>
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<WorkAttach> getWorkAttachByWorkId(String workId) {
		String comId = AppContext.getCurrentComId();
		String condition = "";
		if (StringUtils.isNotBlank(comId))
			condition = "companyId='" + comId + "' and ";
		String hql = "from WorkAttach where " + condition + "workId = ? order by uploadTime desc";
		List<WorkAttach> list = createQuery(hql, workId).list();
		if (list.size() > 0) {
			return list;
		}
		return null;
	}
	/**
	 * 根据附件FileId查询附件 建立日期 : July 16, 2013 5:41:50 PM<br>
	 * 作者 : wangamin<br>
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public WorkAttach getWorkAttachByFileId(String fileId) {
		String comId = AppContext.getCurrentComId();
		String condition = "";
		if (StringUtils.isNotBlank(comId))
			condition = "companyId='" + comId + "' and ";
		String hql = "from WorkAttach where " + condition + "fileId = ?";
		List<WorkAttach> list = createQuery(hql, fileId).list();
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	/**
	 * 
	* @author _zili
	* @Description:如果多个附件一起上传，则会出现保存fileId一样的情况，所以再多传个name进行查询。
	* @date 2015年5月6日 上午10:38:55
	 */
	@SuppressWarnings("unchecked")
	public WorkAttach getWorkAttachByFileIdAndName(String fileId,String realName) {
		List<Object> params = new ArrayList<Object>();
		String comId = AppContext.getCurrentComId();
		String condition = "";
		if (StringUtils.isNotBlank(comId)){
			condition = "companyId = ? and ";
			params.add(comId);
		}
		String hql = "from WorkAttach where " + condition + "fileId = ? and realName = ?";
		params.add(fileId);
		params.add(realName);
		List<WorkAttach> list = createQuery(hql, params.toArray()).list();
		if (list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	
	public void delByWorkId(String workId) {
		String comId = AppContext.getCurrentComId();
		String condition = "";
		if (StringUtils.isNotBlank(comId))
			condition = "companyId='" + comId + "' and ";

		String hql = "delete from WorkAttach where " + condition + " workId=?";
		createQuery(hql, workId).executeUpdate();
	}
	
	/**
	 * 建立日期 : Aug 10, 2013 1:54:23 PM<br>
	 * 作者: 孙耀飞<br>
	 * 模块: 根据条件查询附件 <br>
	 * 描述: realName:实际名字；handlerName:处理者;todoId:待办ID<br>
	 * 1 <br>
	 * 2 <br>
	 */
	@SuppressWarnings("unchecked")
	public List<WorkAttach> getAttachByCondition(String realName,String handlerName,String todoId) { 
	    String comId = AppContext.getCurrentComId();
	    List<Object> params = new ArrayList<Object>();
	    String hql = "from WorkAttach where ";
		if (StringUtils.isNotBlank(comId))
		  hql += "companyId='" + comId + "' and ";
		else
		  hql +=" ";
		if(StringUtils.isNotBlank(realName)){
		  hql +=" and realName = ?";
		  params.add(realName);
		}
		if(StringUtils.isNotBlank(handlerName)){
		  hql +=" and handlerName = ?";
		  params.add(handlerName);
		}
		if(StringUtils.isNotBlank(todoId)){
		  hql +=" and todoId = ?";
		  params.add(todoId);
		}
		hql += " order by uploadTime desc";
		List<WorkAttach> list = createQuery(hql,params.toArray()).list();
		return list;
	}
}